import moment from "moment";

const Util = {};

Util.install = function (Vue, data) {
  const Datas = { ...data };

  /**
   * 字符串是否为空
   **/
  Datas.isNullOrBlank = function (...args) {
    for (var i = 0; i < args.length; i++) {
      var argument = args[i];
      if (argument == null || argument == "" || argument == undefined) {
        return true;
      }
    }
    return false;
  };

  /**
   * 字符串是否为空
   **/
  Datas.arrayRemoveByValue = function (array, val) {
    for (var i = 0; i < array.length; i++) {
      if (array[i] === val) {
        array.splice(i, 1);
        i--;
      }
    }
  };

  /**
   * 设置cookie
   **/
  Datas.setCookie = function (name, value, day) {
    let date = new Date();
    date.setDate(date.getDate() + day);
    document.cookie = name + "=" + value + ";expires=" + date;
  };

  /**
   * 获取cookie
   **/
  Datas.getCookie = function (name) {
    let reg = RegExp(name + "=([^;]+)");
    let arr = document.cookie.match(reg);
    if (arr) {
      return arr[1];
    } else {
      return "";
    }
  };

  /**
   * 删除cookie
   **/
  Datas.delCookie = function (name) {
    setCookie(name, null, -1);
  };

  /**
   * 获取当前时间
   * @returns 当前时间
   */
  Datas.getNowYear = function () {
    let nowDate = new Date();
    return nowDate.getFullYear();
  };

  /**
   * 日期格式化
   * @param {日期} date
   * @param {格式化} format
   * @returns
   */
  Datas.dateFormat = function (date, format) {
    if (Datas.isNullOrBlank(format)) {
      format = "YYYY-MM-DD";
    }
    var result = moment(date).format(format);
    return result;
  };

  /**
   * 获取当前日期时间
   * @returns 当前日期时间
   */
  Datas.nowDateFormat = function (format) {
    let nowDate = new Date();
    return Datas.dateFormat(nowDate, format);
  };

  /**
   * 将 list 数组转化为 treeList
   * 【使用：this.$common.listToTree(dataArray, "id", "pid");】
   *
   * @param {Array} dataList list数据
   * @param {string} id 节点key值
   * @param {string} pId 父节点key值
   * @returns {Array} treeList
   */
  Datas.listToTree = function (dataList, id, pId) {
    const data = JSON.parse(JSON.stringify(dataList));
    const result = [];
    if (!Array.isArray(data)) {
      return result;
    }
    data.forEach((item) => {
      delete item.children;
    });
    const map = {};
    data.forEach((item) => {
      map[item[id]] = item;
    });
    data.forEach((item) => {
      const parent = map[item[pId]];
      if (parent) {
        (parent.children || (parent.children = [])).push(item);
      } else {
        result.push(item);
      }
    });
    return result;
  };

  /**
   * 将 list 数组转化为 treeList
   * 【使用：this.$common.listToTreeMaterial(dataArray, "id", "pid");】
   *
   * @param {Array} dataList list数据
   * @param {string} id 节点key值
   * @param {string} pId 父节点key值
   * @returns {Array} treeList
   */
  Datas.listToTreeMaterial = function (dataList, id, pId) {
    const data = JSON.parse(JSON.stringify(dataList));
    const result = [];
    if (!Array.isArray(data)) {
      return result;
    }
    data.forEach((item) => {
      delete item.children;
    });
    const map = {};
    data.forEach((item) => {
      map[item[id]] = item;
    });
    data.forEach((item) => {
      const parent = map[item[pId]];
      if (parent) {
        (parent.children || (parent.children = [])).push(item);
      } else {
        result.push(item);
      }
    });
    setTreeMaterial(result);
    return result;
  };

  function setTreeMaterial(datas) {
    //遍历树  获取id数组
    for (var i in datas) {
      if (datas[i].pids && datas[i].pids.split(",")) {
        var pidsCount = datas[i].pids.split(",").length;
        datas[i].depth = pidsCount - 1;
      }
      if (datas[i].children) {
        //存在子节点就递归
        setTreeMaterial(datas[i].children);
      } else {
        datas[i].leaf = true;
      }
    }
  }

  Vue.prototype.$common = Datas; // 挂在原型上，方便使用
};

export default Util;
